﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

public static class RegCondition
{
    #region Regexs
    static string reAlpha = "[a-zA-Z|\\s]+";
    static string reANum = "[0-9a-zA-Z|\\s]+";
    static string reMail = @"^(([^<>()[\]\\.,;:\s@\""]+"
                    + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                    + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                    + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                    + @"[a-zA-Z]{2,}))$";
    static string reIP = "[0-9]{1,3}\x2E[0-9]{1,3}\x2E[0-9]{1,3}\x2E[0-9]{1,3}(:[0-9]{1,5})?";
    static string reWeb = "([http|HTTP|ftp|FTP|https]://)?([w|W]{3}\x2E)?[0-9a-zA-Z]\x2E[a-zA-Z]{2,3}(\x2E[a-zA-Z]{2,3})?(/[0-9a-zA-Z|.]+)*";
    static string reTel = "\\+?[0-9]{4}[0-9]*";
    static string reNum = "-?[0-9]+";
    static string reCoord = "-?[0-9]{1,3}(\x2E[0-9]{0,5})?";
    static string reHex = "[0-9a-fA-F]+";
    static string reAddress = "[0-9a-zA-Z|\\s|º]+";
    #endregion

    public static bool Condition(TextBox text, RegularExpressions re)
    {
        return pCondition(text, re, "", "") || !text.Visible;
    }

    public static bool Condition(TextBox text, RegularExpressions re, int minVal, int maxVal)
    {
        return pCondition(text, re, minVal.ToString(), maxVal.ToString());
    }

    private static bool pCondition(TextBox text, RegularExpressions re, string minVal, string maxVal)
    {
        Regex regExp = getRegex(re);

        if (minVal == "" && maxVal == "")
            return regExp.IsMatch(text.Text);
        if (minVal != "" && maxVal != "")
            return (regExp.IsMatch(text.Text) && int.Parse(text.Text) >= int.Parse(minVal) && int.Parse(text.Text) <= int.Parse(maxVal));
        return false;
    }

    private static Regex getRegex(RegularExpressions re)
    {
        switch (re)
        {
            case RegularExpressions.Numeric:
                return new Regex(reNum);
            case RegularExpressions.Alphabet:
                return new Regex(reAlpha);
            case RegularExpressions.AlphaNumeric:
                return new Regex(reANum);
            case RegularExpressions.eMail:
                return new Regex(reMail);
            case RegularExpressions.IP:
                return new Regex(reIP);
            case RegularExpressions.Web:
                return new Regex(reWeb);
            case RegularExpressions.Telephone:
                return new Regex(reTel);
            case RegularExpressions.Coordinates:
                return new Regex(reCoord);
            case RegularExpressions.Hexadecimal:
                return new Regex(reHex);
            case RegularExpressions.Address:
                return new Regex(reAddress);
        }
        return null;
    }

    public enum RegularExpressions
    {
        Numeric,
        Alphabet,
        AlphaNumeric,
        eMail,
        IP,
        Web,
        Telephone,
        Coordinates,
        Hexadecimal,
        Address
    }
}
